﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Calibration.CCoCalibrationXmlMergeCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections;
using System.IO;
using System.Xml;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.Calibration;

public class CCoCalibrationXmlMergeCmd : ITxCommand, ITxExecutableCommand
{
  private TxCommandEnabler m_enabler = new TxCommandEnabler(true);

  public ITxCommandEnabler CommandEnabler => (ITxCommandEnabler) this.m_enabler;

  public bool Connect() => true;

  public void Disconnect()
  {
  }

  public void Execute(object cmdParams)
  {
    if (!(cmdParams is ArrayList arrayList1) || arrayList1.Count != 2)
      return;
    ArrayList arrayList2 = arrayList1[0] as ArrayList;
    string nodeExtId = arrayList1[1] as string;
    if (arrayList2 == null || nodeExtId == null || !(nodeExtId != ""))
      return;
    CApCalibrationXmlMerger calibrationXmlMerger = new CApCalibrationXmlMerger();
    CApCalibrationSetsXmlManager.GetEngineeringDataFolderPath(nodeExtId);
    for (int index = 0; index < arrayList2.Count; ++index)
    {
      string calibrationXmlFilePath = CApCalibrationSetsXmlManager.GetCalibrationXmlFilePath(arrayList2[index] as string);
      CApCalibrationSetsXmlManager.GetEngineeringDataFolderPath(arrayList2[index] as string);
      if (calibrationXmlFilePath != null && File.Exists(calibrationXmlFilePath))
      {
        XmlDocument doc = new XmlDocument();
        doc.Load(calibrationXmlFilePath);
        calibrationXmlMerger.Append(doc);
      }
    }
    string calibrationXmlFilePath1 = CApCalibrationSetsXmlManager.GetCalibrationXmlFilePath(nodeExtId);
    this.SaveDocument(calibrationXmlMerger.Document, calibrationXmlFilePath1);
  }

  private bool SaveDocument(XmlDocument doc, string file)
  {
    bool flag = false;
    try
    {
      doc.Save(file);
      flag = true;
    }
    catch (DirectoryNotFoundException ex)
    {
      TxErrorStack.Push("CALIBRATION_SAVE_XML_TO_FILE_FAILED", file);
    }
    catch (IOException ex)
    {
      TxErrorStack.Push("CALIBRATION_SAVE_XML_TO_FILE_FAILED", file);
    }
    catch (Exception ex)
    {
      TxErrorStack.Push("CALIBRATION_SAVE_XML_TO_FILE_FAILED", file);
    }
    return flag;
  }

  public bool Expose => true;

  public string Identifier => "DnProcessSimulateCommands.Calibration.CCoCalibrationXmlMergeCmd";
}
